package org.wyf.EveryPoint.crowd.controller;

import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.crowd.entity.Comment;
import org.wyf.EveryPoint.crowd.service.ICommentService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 留言表 Controller
 *
 * @author wyf
 * @date 2021-04-26 23:08:41
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class CommentController extends BaseController {

    private final ICommentService commentService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "comment")
    public String commentIndex(){
        return FebsUtil.view("comment/comment");
    }

    @GetMapping("comment")
    @ResponseBody
    @RequiresPermissions("comment:list")
    public FebsResponse getAllComments(Comment comment) {
        return new FebsResponse().success().data(commentService.findComments(comment));
    }

    @GetMapping("comment/list")
    @ResponseBody
    @RequiresPermissions("comment:list")
    public FebsResponse commentList(QueryRequest request, Comment comment) {
        Map<String, Object> dataTable = getDataTable(this.commentService.findComments(request, comment));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Comment", exceptionMessage = "新增Comment失败")
    @PostMapping("comment")
    @ResponseBody
    @RequiresPermissions("comment:add")
    public FebsResponse addComment(@Valid Comment comment) {
        this.commentService.createComment(comment);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Comment", exceptionMessage = "删除Comment失败")
    @GetMapping("comment/delete")
    @ResponseBody
    @RequiresPermissions("comment:delete")
    public FebsResponse deleteComment(Comment comment) {
        this.commentService.deleteComment(comment);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Comment", exceptionMessage = "修改Comment失败")
    @PostMapping("comment/update")
    @ResponseBody
    @RequiresPermissions("comment:update")
    public FebsResponse updateComment(Comment comment) {
        this.commentService.updateComment(comment);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Comment", exceptionMessage = "导出Excel失败")
    @PostMapping("comment/excel")
    @ResponseBody
    @RequiresPermissions("comment:export")
    public void export(QueryRequest queryRequest, Comment comment, HttpServletResponse response) {
        List<Comment> comments = this.commentService.findComments(queryRequest, comment).getRecords();
        ExcelKit.$Export(Comment.class, response).downXlsx(comments, false);
    }

    /**
     * 获取评论
     * @param projectId
     * @return
     */
    @ControllerEndpoint(operation = "获取评论", exceptionMessage = "获取评论失败")
    @PostMapping("/web/getComment")
    @ResponseBody
    public FebsResponse getComment(Long projectId) {
        List<Comment> comments = commentService.listCommentsByPid(projectId);
        return new FebsResponse().success().data(comments);
    }
    @ControllerEndpoint(operation = "保存评论", exceptionMessage = "保存评论失败")
    @PostMapping("/web/saveComment")
    @ResponseBody
    public FebsResponse saveComment(String detail,Long projectId) {
        Comment comment = new Comment();
        comment.setDetail(detail);
        comment.setPid(projectId);
        comment.setUid(getCurrentUser().getUserId());
        comment.setCreateTime(new Date());
        commentService.save(comment);
        return new FebsResponse().success();
    }
}
